package com.eastfair.matching.service.impl;

import com.eastfair.auth.api.RoleServiceFeign;
import com.eastfair.auth.dto.RoleDTO;
import com.eastfair.auth.vo.RoleVo;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.matching.service.AuthMatchRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 远程角色服务.
 *
 * @author dqq
 * @date 2022 /7/25
 */
@Service
@Slf4j
public class AuthMatchRoleServiceImpl implements AuthMatchRoleService {

    @Resource
    private RoleServiceFeign roleServiceFeign;

    @Override
    public List<RoleVo> queryRole(RoleDTO roleDTO) {
        R<List<RoleVo>> r = roleServiceFeign.queryRole(roleDTO);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        return r.getData();
    }

    @Override
    public RoleVo getRoleByBusinessTag(String businessTag) {
        RoleDTO roleDTO = new RoleDTO();
        roleDTO.setBusinessTag(businessTag);
        List<RoleVo> roleVos = queryRole(roleDTO);
        if (roleVos == null || roleVos.isEmpty()) {
            throw BizException.wrap(-1, "查询配套服务商角色失败");
        }
        return roleVos.get(0);
    }
}